package com.szboanda.parser.impl;

import com.szboanda.parser.ISqlParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ：zhangsheng
 * @date ：Created in 2020/2/24 10:35
 * @description：字段有无标签解析, 例如： <TAG> AND SO2 > 3 </TAG>
 * @modified By：
 * @version: 1.0
 */
public class HasFieldTagParserImpl implements ISqlParser {


    /**
     * 标签前半部分匹配：
     */
    private static final Pattern START_PATTERN = Pattern.compile("<\\s*(?<field>\\w{1,20})\\s*>");

    /**
     * 全标签匹配：
     */
    private static final String TAG_PATTERN = "<startTag>([^/]*)</\\s*<field>\\s*>";

    @Override
    public String parse(String sqlScript, List<Object> sqlArgs, Map<String, ?> context) {
        List<HasTag> tagLst = new ArrayList<HasTag>(8);
        Matcher match = START_PATTERN.matcher(sqlScript);
        while (match.find()) {
            String key = match.group(0);
            String field = match.group("field").trim();
            Pattern tagPattern = Pattern.compile(TAG_PATTERN.replace("<startTag>", key).replace("<field>", field));
            Matcher tagMatch = tagPattern.matcher(sqlScript);
            while (tagMatch.find()) {
                tagLst.add(new HasTag(field, tagMatch.group(1), tagMatch.group(0)));
            }
        }
        for (HasTag hasTag: tagLst){
            if (null != context.get(hasTag.getTagName())) {
                Object val = context.get(hasTag.getTagName());
                if (val instanceof String) {
                    String strVal = (String) val;
                    if (strVal != "") {
                        sqlScript = sqlScript.replace(hasTag.sqlScript, hasTag.showContent);
                    } else {
                        sqlScript = sqlScript.replace(hasTag.sqlScript, "");
                    }
                } else if (val instanceof List) {
                    List lstVal = (List) val;
                    if (lstVal.isEmpty()) {
                        sqlScript = sqlScript.replace(hasTag.sqlScript, "");
                    } else {
                        sqlScript = sqlScript.replace(hasTag.sqlScript, hasTag.showContent);
                    }
                } else {
                    sqlScript = sqlScript.replace(hasTag.sqlScript, hasTag.showContent);
                }
            } else {
                sqlScript = sqlScript.replace(hasTag.sqlScript, "");
            }
        }
        return sqlScript;
    }


    class HasTag{
        /**
         * 名称
         */
        private String tagName;

        /**
         *  被显示内容
         */
        private String showContent;

        /**
         * sql脚本
         */
        private String sqlScript;

        HasTag(String tagName, String showContent, String sqlScript){
            this.tagName = tagName;
            this.showContent = showContent;
            this.sqlScript = sqlScript;
        }


        public String getTagName() {
            return tagName;
        }

        public void setTagName(String tagName) {
            this.tagName = tagName;
        }

        public String getShowContent() {
            return showContent;
        }

        public void setShowContent(String showContent) {
            this.showContent = showContent;
        }

        public String getSqlScript() {
            return sqlScript;
        }

        public void setSqlScript(String sqlScript) {
            this.sqlScript = sqlScript;
        }

        @Override
        public String toString() {
            return "tagName=" + this.tagName + "\n" + "showContent=" + this.showContent + "\n"+ "sqlScript=" + this.sqlScript + "\n";
        }
    }


}
